package jmeterBusiness;

import com.alibaba.fastjson.JSONObject;
import models.MyConfig;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by yangxianming on 2018/12/14.
 * 3号工厂处理jmeter业务
 * jmeter命令行 D:\xxxx\apache-jmeter-3.1\bin\jmeter -n -t D:\xxxxx\appList.jmx -l D:\xxxx\appList.jtl -e -o D:\xxxx\appList
 */
public class No3Factory extends Thread{
    private MyConfig myConfig;
    public JSONObject jmeterPara;

    public Process getJmeterProcess() {
        return jmeterProcess;
    }

    public void setJmeterProcess(Process jmeterProcess) {
        this.jmeterProcess = jmeterProcess;
    }

    private String command = "";     //启动jmeter的命令行，同时也用来判断时是否有jmeter在运行

    public String getCommand() {
        return command;
    }

    public void setCommand(String command) {
        this.command = command;
    }

    private Process jmeterProcess;    //用来判断是否有jmeter程序还在执行

    @Override
    public synchronized void run() {
        Runtime runtime = Runtime.getRuntime();
        try {
            System.out.println("command:" + command);
            Process process = runtime.exec(command);
            InputStream inputStream = process.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line, r = "";
            while ((line = bufferedReader.readLine()) != null){
                System.out.println(line);
                r = r + line;
                if (r.contains("errorlevel")){
                    System.out.println("----find err---end process----");
                    process.destroy();
                    command = "";
                }
                if (r.contains("Could not open")){
                    System.out.println("----Could not open script----");
                    process.destroy();
                    command = "";
                }
            }
            System.out.println("程序执行完毕");
            command = "";
        }catch (Exception e){
            e.printStackTrace();
            command = "";
        }

    }
}
